# author = '李天宝'
# write in '2020/8/4 0004 15:16'

from rest_framework.views import APIView
from rest_framework.response import Response
# from rest_framework import serializers
# from rest_framework.pagination import CursorPagination

from products.models import goods_info


class GoodList(APIView):
    """
    这个模型类是一次返回10条数据的商品信息类,
    可以自己定义返回第几页，这一页返回多少条数据
    :param page_size=10 返回多少条数据 page=x 第几页
    """
    def get(self,request):
        page,page_size=1,10
        if 'page_size' in request.query_params:
            page_size=request.query_params['page_size']
        if 'page' in request.query_params:
            page=request.query_params['page']

        page=page-1 #方便计数
        start=page*page_size
        end=(page+1)*page_size
        goods=goods_info.objects.all()[start:end]

        return Response({
            'code':200,
            'length':len(goods),
            'detail':goods.values('id','name','intro','cover_img','selling_price','original_price'),
        })
